package com.artisan.admin.controllers;

import com.artisan.admin.model.adViewModels.AdViewModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.admin.resultModels.adResultModels.AdResultModel;
import com.artisan.domain.dto.AdDto;
import com.artisan.domain.dto.PageDto;
import com.artisan.domain.service.FileService;
import com.artisan.domain.serviceInterface.IAdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 广告控制器
 */
@RequestMapping("/api/ad")
@RestController
@Api("广告模块")
@ApiOperation(value = "广告模块")
public class AdController {
    private final IAdService adService;
    private final FileService fileService;

    public AdController(IAdService adService, FileService fileService) {
        this.adService = adService;
        this.fileService = fileService;
    }

    /**
     * 获得广告列表
     * @param name 名称
     * @param pageIndex 当前页
     * @param pageSize 每页数
     */
    // GET /list
    @ApiOperation(value = "获得广告列表")
    @ApiResponses(@ApiResponse(code = 200, response = AdResultModel.class, message = "成功"))
    @RequestMapping(value ={"/list"}, method = {RequestMethod.GET})
    public MessageBase getList(@RequestParam(value = "name")String name, @RequestParam(value = "pageIndex")int pageIndex, @RequestParam(value = "pageSize")int pageSize) {
        MessageBase result = new MessageBase();
        PageDto<AdDto> page = this.adService.findList(name, pageIndex, pageSize);
        result.setPageIndex(page.getNumber() + 1);
        result.setPageSize(page.getSize());
        result.setTotalRecord(Math.toIntExact(page.getTotalElements()));

        ArrayList<AdResultModel> models  = new ArrayList<>();

        page.getContent().forEach(dto -> {
            AdResultModel model = new AdResultModel();
            model.setId(dto.getId());
            model.setClickNum(dto.getClickNum());
            model.setName(dto.getName());
            model.setPhotoUrl(dto.getPhotoUrl());

            models.add(model);
        });

        result.setData(models);
        return result;
    }

    /**
     * 添加广告
     */
    // POST /add
    @ApiOperation(value = "添加广告")
    @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
    @RequestMapping(value ={"/add"}, method = {RequestMethod.POST})
    public MessageBase postAdd(@RequestBody AdViewModel model) throws IOException {
        MessageBase result = new MessageBase();

        AdDto dto = new AdDto();
        dto.setName(model.getName());
        dto.setRegion(model.getRegion());
        dto.setStatus(model.getStatus());
        dto.setPhotoUrl(model.getPhotoUrl());
        dto.setTypeId(model.getTypeId());
        dto.setLink(model.getLink());
        dto.setContent(model.getContent());
        dto.setAdPositionId(model.getAdPositionId());
        int id = this.adService.add(dto);

        result.setSuccess(id > 0);

        return result;
    }
}
